package com.film.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.film.dao.UserDao;
import com.film.dao.impl.UserDaoImpl;
import com.film.domain.User;
import com.film.service.UserService;

public class UserServiceImpl implements UserService {
	
	private UserDao userDao = new UserDaoImpl();

	@Override
	public int register(String username, String password, String phoneNum, String nickName) {
		User user = userDao.selectOne(username);
		if(user == null) {
			user = userDao.selectOneByPhone(phoneNum);
			if(user == null) {
				//没有用户注册过，可以注册
				System.out.println("可以注册");
				user = new User(username,password,phoneNum,nickName);
				return userDao.insertUser(user);
			}
			return 2;
		}
		return 0;
	}

	@Override
	public Map<String, Object> login(String username, String password) {
		// TODO Auto-generated method stub
				Map<String,Object> map = new HashMap();
				User u= userDao.selectOneUser(username, password);
				if(u!=null) {
					map.put("user",u);
					map.put("msg", "登录成功");
				}else {
					User user = userDao.selectOne(username);
					if(user!=null) {
						map.put("msg", "密码错误");
					}else {
						map.put("msg", "用户名错误");
					}
				}
				return map; 
	}

	@Override
	public int updateById(User user) {
		
		return userDao.updateById(user);
	}

	@Override
	public User selectAll(int u_id) {
		
		return userDao.selectALl(u_id);
	}

	@Override
	public int updateImg(int uId, String imgPath) {
		
		return userDao.updateImg(uId, imgPath);
	}










	@Override
	public List<User> selectALl() {
		
		return userDao.selectALl();
	}
	
	
	
	
	
	@Override
	public int deleteUser(int uId) {
		if(userDao.deleteUser(uId)==0) {
			System.out.println("删除失败");
			return 0;
		}else {
			System.out.println("删除成功");
			return 1;
		}
		
	}

	@Override
	public List<User> selectlimit(int a) {
		int stripNum=7;//默认分页的查询条数为7条
		int pageNums=0;
		if(a==0) {
			 pageNums=0;
		}else {
			 pageNums=(a-1)*stripNum;
		}
		
		return userDao.selectlimit(pageNums, stripNum);
	}

}
